<template>
  <div>
    <div class="top">
      <el-form :inline="true">
        <el-form-item label="商品类型搜索：">
          <el-input v-model="data.skuName" placeholder="请输入" style="width:180px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="inquire">
            <i class="el-icon-search" />
            查询</el-button>

        </el-form-item>
      </el-form>
    </div>
    <div class="list">
      <el-button type="warning" @click="xinjian"> <i class="el-icon-circle-plus-outline" /> 新建</el-button>
      <el-button>导入数据</el-button>
      <!-- 列表 -->
      <el-table :data="list">
        <el-table-column type="index" label="序号">
          <template scope="scope">
            <span>{{ (data.pageIndex-1)*data.pageSize+scope.$index+1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品名称">
          <template v-slot="{ row }">
            <span>{{ row ?.skuName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品图片">
          <template v-slot="{ row }">
            <img class="rowimg" :src="row.skuImage" alt="">
          </template>
        </el-table-column>
        <el-table-column label="品牌">
          <template v-slot="{ row }">
            <span>{{ row ?.brandName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规格">
          <template v-slot="{ row }">
            <span>{{ row ?.unit }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品价格">
          <template v-slot="{ row }">
            <span>{{ row ?.price }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品类型">
          <template v-slot="{ row }">
            <span>{{ row ?.skuClass?.className }}</span>
          </template>
        </el-table-column>
        <el-table-column width="170px" label="创建日期">
          <template v-slot="{ row }">
            <span>{{ $dayjs(row ?.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="xinjian(row)"><span class="xiugai">修改</span> </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-row type="flex" style="height:60px" align="middle" justify="space-between">
        <span class="record">共{{ totalCount }}条记录 第{{ data.pageIndex }}/ {{ totalPage }} 页</span>
        <el-pagination
          :page-size="data.pagesize"
          :current-page="data.pageIndex"
          layout="prev,  next"
          :total="+totalCount"
          prev-text="上一页"
          next-text="下一页"

          @current-change="changePage"
        />
      </el-row>
    </div>
    <el-dialog
      title="新增商品类型"
      :visible.sync="dialogShow"
      width="45%"
      :before-close="handleClose"
    >
      <el-form label-width="110px">
        <el-form-item label="商品名称：">
          <el-input v-model="Bodys.skuName" placeholder="请输入" maxlength="15" show-word-limit style="width: 450px;" />
        </el-form-item>
        <el-form-item label="品牌：">
          <el-input v-model="Bodys.brandName" placeholder="请输入" maxlength="10" show-word-limit style="width: 450px;" />
        </el-form-item>
        <el-form-item label="商品价格：">
          <el-input-number v-model="Bodys.price" controls-position="right" :step="0.5" style="width: 450px;" :min="0" />
        </el-form-item>
        <el-form-item label="商品类型：">
          <el-select v-model="Bodys.classId" style="width: 450px;" placeholder="请选择">
            <el-option
              v-for="item in lists"
              :key="item.classId"
              :label="item.className"
              :value="item.classId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="规格：">
          <el-input v-model="Bodys.unit" placeholder="请输入" maxlength="11" show-word-limit style="width: 450px;" />
        </el-form-item>
        <el-form-item label="头像：">
          <ImageUpload v-model="Bodys.skuImage" />
          <span style="color: #dbdfe5;">支持扩展名：jpg、png，文件不得大于100kb</span>
        </el-form-item>
        <el-form-item>
          <div class="dialogboxfood">
            <el-button @click="qxpuits">取消</el-button>
            <el-button class="footBtn" type="warning" @click="posttijiao">确定</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import ImageUpload from '@/views/department/router/components/image-upload.vue'
import { GETshangpin, getShangpinleixing, POSTshangpin, PUTshangpin } from '@/api/approval'
export default {
  name: 'Approval2',
  components: { ImageUpload },
  data() {
    return {
      dialogShow: false,
      // 获取列表数据
      data: {
        pageIndex: 1,
        pageSize: 10,
        skuName: ''
      },
      dataList: {
        pageIndex: 1,
        pageSize: 10000000,
        className: ''
      },
      // 列表数据
      lists: [],
      // 总数据
      totalCount: '',
      // 总页数
      totalPage: '',
      // 列表数据
      list: [],
      pointLocation: {
        pageIndex: 1,
        pageSize: 10000000
      },
      // 售货机状态映射表
      vmStatuslist: {
        0: '未投放',
        1: '运营',
        3: '撤机'
      },
      // 人员列表
      renyuanList: [],
      // 人员详情
      xiangqingList: [],
      // 新增商品类型
      className: '',
      skuId: null,
      Bodys: {
        skuName: '',
        skuImage: '',
        price: '',
        classId: '',
        unit: '',
        brandName: ''
      }
    }
  },
  created() {
    // 页面列表
    this.getEquipmentList()
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.Bodys = {}
      this.dialogShow = false
    },
    // 分页
    changePage(newPage) {
      // console.log(newPage)
      this.data.pageIndex = newPage
      // console.log(this.data)
      this.getEquipmentList()
    },
    // 获取列表数据
    async getEquipmentList() {
      console.log(this.data)
      const res = await GETshangpin(this.data)
      this.list = res.currentPageRecords
      const arr = await getShangpinleixing(this.data)
      this.lists = arr.currentPageRecords
      this.totalCount = res.totalCount
      this.totalPage = res.totalPage
      console.log(this.list)
      console.log(this.lists)
    },
    // 查询重新渲染页面
    inquire() {
      this.getEquipmentList()
    },
    async xinjian(row) {
      if (row) {
        this.Bodys.skuName = row.skuName
        this.Bodys.skuImage = row.skuImage
        this.Bodys.price = row.price
        this.Bodys.classId = row?.skuClass?.classId
        this.Bodys.unit = row.unit
        this.Bodys.brandName = row.brandName
        console.log(this.Bodys)
        this.skuId = row.skuId
      }
      this.dialogShow = true
    },
    qxpuits() {
      this.Bodys = {}
      this.dialogShow = false
    },
    async posttijiao() {
      if (!this.skuId) {
        await POSTshangpin(this.Bodys)
        this.$message.success('新建成功')
      } else {
        await PUTshangpin(this.Bodys, this.skuId)
        this.$message.success('修改成功')
      }
      this.dialogShow = false
      this.getEquipmentList()
      this.className = ''
      this.classId = null
    }

  }
}
</script>

<style  lang='scss' scoped>
.selected{
position: relative;
    width: 134px;
    height: 134px;
    padding-top: 16px;
    background-color: #f6f7fb;
    -webkit-box-shadow: 0 2px 4px 0 rgba(0,0,0,.06);
    box-shadow: 0 2px 4px 0 rgba(0,0,0,.06);
    border-radius: 4px;
    text-align: center;
    cursor: pointer;
}
 .top{
    height: 80px;
    margin: 20px;
    padding: 18px;
    background-color: #fff;
  }
    .list{
    padding: 18px;
    margin: 20px;
    background-color: #fff;
   }
    ::v-deep.el-pagination .btn-next{
  background-color: #d5ddf8 !important;
  width: 70px;
  height: 30px;
  text-align: center;
  border-radius: 3px;
   margin: 10px;
}

::v-deep.el-pagination .btn-prev{
  background-color: #d5ddf8 !important;
  width: 70px;
  height: 30px;
  text-align: center;
  border-radius: 3px;
  margin: 10px;
}
.record{
  color: #dbdfe5;
}
.xiugai{
  color: #6eabfe;
}
.sahnchu{
  color: #fb9782;
}
.rowimg{
      display: block;
    width: 24px;
    height: 24px;
    -o-object-fit: cover;
    object-fit: cover;
    border-radius: 50%;
    background: #f3f6fb;
    border: 1px solid #f3f6fb;
}
  .avatar-uploader .el-upload {
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 2px dashed  #b7b7b7 !important;
    font-size: 28px;
    color: #8c939d;
    background-color: #f3f6fb;
    width: 100px;
    height:100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    // border: 2px dashed  #000 !important;
    width: 100px;
    height:100px;
    display: block;
  }
  .dialogboxfood{
    width: 70%;
    display: flex;
    justify-content: center;
    .footBtn{
      margin-left: 60px;
    }
  }
.rowimg{
      display: block;
    width: 24px;
    height: 24px;
    -o-object-fit: cover;
    object-fit: cover;
    border-radius: 50%;
    background: #f3f6fb;
    border: 1px solid #f3f6fb;
}
</style>
